﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using Evento.Models;

namespace Evento.Controllers
{ 
    public class PlacesController : Controller
    {
		#region Fields 

        private readonly EventoDB _eventoDB;

		#endregion Fields 

		#region Constructors 

        /// <summary>
        /// Initializes a new instance of the <see cref="PlacesController"/> class.
        /// </summary>
        public PlacesController()
        {
            _eventoDB = new EventoDB();
        }

		#endregion Constructors 

		#region Methods 

        //
        // GET: /Places/Create
        public ActionResult Create()
        {
            return View();
        }

         //
        // POST: /Places/Create
        [HttpPost]
        public ActionResult Create(Place place)
        {
            if (ModelState.IsValid)
            {
                place.Guid = "{" + Guid.NewGuid().ToString().ToUpper() + "}";
                _eventoDB.Places.Add(place);
                _eventoDB.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(place);
        }

        //
        // GET: /Places/Delete/5
        public ActionResult Delete(int id)
        {
            Place place = _eventoDB.Places.Find(id);
            return View(place);
        }

        //
        // POST: /Places/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Place place = _eventoDB.Places.Find(id);
            _eventoDB.Places.Remove(place);
            _eventoDB.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /Places/Details/5
        public ViewResult Details(int id)
        {
            Place place = _eventoDB.Places.Find(id);
            return View(place);
        }

        //
        // GET: /Places/Edit/5
        public ActionResult Edit(int id)
        {
            Place place = _eventoDB.Places.Find(id);
            return View(place);
        }

        //
        // POST: /Places/Edit/5
        [HttpPost]
        public ActionResult Edit(Place place)
        {
            if (ModelState.IsValid)
            {
                _eventoDB.Entry(place).State = EntityState.Modified;
                _eventoDB.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(place);
        }

        //
        // GET: /Places/
        public ViewResult Index()
        {
            return View(_eventoDB.Places.ToList());
        }
        protected override void Dispose(bool disposing)
        {
            _eventoDB.Dispose();
            base.Dispose(disposing);
        }

		#endregion Methods 
    }
}